package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.MedicationReactionService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.masterData.MedicationReactionDelDto;
import com.ccys.db.dto.masterData.MedicationReactionPageQueryDto;
import com.ccys.db.dto.masterData.MedicationReactionSaveDto;
import com.ccys.db.entity.TblMedicationReaction;
import com.ccys.db.mapper.TblMedicationReactionMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.MedicationReactionPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 药物反应实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 13:24
 **/

@Service
public class MedicationReactionServiceImpl extends BaseServiceImpl<TblMedicationReaction> implements MedicationReactionService {

    @Resource
    private TblMedicationReactionMapper tblMedicationReactionMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(MedicationReactionSaveDto dto) {
        //校验 - 同一个集团 - 同一个名称 - 不允许重复
        if (ObjectUtil.isNotEmpty(dto.getName()) && ObjectUtil.isNotEmpty(dto.getGroupId())) {
            Example example = new Example(TblMedicationReaction.class);
            Example.Criteria criteria = example.createCriteria().andEqualTo("groupId", dto.getGroupId())
                    .andEqualTo("name", dto.getName()).andEqualTo("delFlag",0);
            //校验如果存在ID - 需要排除掉自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                criteria.andNotEqualTo("id", dto.getId());
            }
            //开始查询-并判断
            if (ObjectUtil.isNotEmpty(tblMedicationReactionMapper.selectOneByExample(example))) {
                return ResponseResult.error("The name already exists in the current group!");
            }
        }
        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }
        //以上校验通过 - 开始操作 - 新增或修改 - 并且直接判断好直接返回
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult delete(MedicationReactionDelDto dto) {
        //判断id不为空
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备参数并软删除修改数据库
        TblMedicationReaction reaction = new TblMedicationReaction();
        reaction.setId(dto.getId());
        reaction.setDelFlag(1);
        if (tblMedicationReactionMapper.updateByPrimaryKeySelective(reaction) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findPage(MedicationReactionPageQueryDto dto) {
        //检查并设置值
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        PageInfo<MedicationReactionPageQueryVo> info = new PageInfo<>(tblMedicationReactionMapper.findPage(dto));
        return ResponseResult.success(info);
    }
}
